import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '../views/HomePage/index.vue'
import LayOuts from '../LayOuts/index.vue'
import { isLogin } from '@/utils/login'
import { ElMessage } from 'element-plus'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '首页',
      component: LayOuts,
      redirect: '/home',
      children: [
        // 首页   
        {
          path: '/home',
          name: '首页',
          component: () => import('../views/HomePage/index.vue'),
          children: [

            {
              path: '/home',
              name: '首页',
              component: () => import('../views/HomePage/index.vue'),
            }
          ]
        },

        // 药品管理页面
        {
          path: '/drugsIndex',
          name: '药品管理',
          redirect: '/drugsIndex/classification',
          component: () => import('../views/DrugsIndex/index.vue'),
          children: [
            {
              path: '/drugsIndex/classification',
              name: '分类管理',
              component: () => import('../views/DrugsIndex/Classification/index.vue')
            },
            {
              path: '/drugsIndex/symptom',
              name: '症状管理',
              component: () => import('../views/DrugsIndex/Symptom/index.vue')
            },
            {
              path: '/drugsIndex/drugsMana',
              name: '药品管理',
              component: () => import('../views/DrugsIndex/DrugsMana/index.vue'),
            },
            {
              path: '/drugsIndex/verifyDrugs',
              name: '药品审核',
              component: () => import('../views/DrugsIndex/VerifyDrugs/index.vue')
            },

          ]
        },

        // 订单管理页面
        {
          path: '/orderManagement',
          name: '订单管理',
          // redirect: '/orderManagement/orderLists',
          component: () => import('../views/OrderManagement/index.vue'),
          children: [
            {
              path: '/orderManagement/orderLists',
              name: '药品订单',
              component: () => import('../views/OrderManagement/OrderLists/index.vue')
            },
            {
              path: '/drugsIndex/exitMoney',
              name: '退款申请',
              component: () => import('../views/OrderManagement/ExitMoney/index.vue')
            },
            {
              path: '/drugsIndex/drugsMana',
              name: '拼团订单 ',
              component: () => import('../views/DrugsIndex/DrugsMana/index.vue')
            },
          ]
        },
        // 优惠促销
        {
          path: '/promotionIndex',
          name: '促销',
          component: () => import('../views/PromotionIndex/index.vue')
        },
        // 用户页面
        {
          path: '/userLists',
          name: '用户',
          component: () => import('../views/UserLists/index.vue')
        },
        // 设置页面
        {
          path: '/settingIndex',
          name: '设置',
          component: () => import('../views/SettingIndex/index.vue')
        },
        // 内容 页面
        {
          path: '/contentIndex',
          name: '内容',
          component: () => import('../views/ContentIndex/index.vue')
        },
        // 权限页面
        {
          path: '/authorityIndex',
          name: '权限',
          component: () => import('../views/AuthorityIndex/index.vue')
        },
        // 用户设置 页面
        {
          path: '/accountSettings',
          name: '用户设置',
          component: () => import('../views/AccountSettings/index.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginPage/index.vue')
    },
    {
      path: "/:path(.*)",
      name: 'NotFound',
      component: () => import("../views/NotFound/index.vue")
    },
  ]
})

// 路由登录鉴权
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (to.path !== '/login') {
    if (isLogin()) {
      next()
    } else {
      next('/login')
      // ElMessage({
      //   message: '登录过期或已失效，请重新登录！',
      //   type: 'error',
      //   duration: 2000,
      //   onClose: () => {
      //     next('/login')
      //   }
      // })
    }
  } else {
    next()
  }
})


export default router
